﻿#pragma once
#include<stdio.h>
#include<vector>
#include<iostream>
using namespace std;
namespace hash_bucket
{

	template<class K>
	struct HashFunc
	{
		size_t operator()(const K& key)
		{
			return (size_t)key;
		}
	};

	// 哈希表中支持字符串的操作
	template<>
	struct HashFunc<string>
	{
		size_t operator()(const string& key)
		{
			size_t hash = 0;
			for (auto e : key)
			{
				hash *= 31;
				hash += e;
			}

			return hash;
		}
	};


	template<class T>
	struct HashNode
	{
		T _data;
		HashNode<T>* _next;
		HashNode(const T& data)
			:_data(data)
			, _next(nullptr)
		{}
	};

	inline unsigned long __stl_next_prime(unsigned long n)
	{
		// Note: assumes long is at least 32 bits.
		static const int __stl_num_primes = 28;
		static const unsigned long __stl_prime_list[__stl_num_primes] =//数组
		{
		   53, 97, 193, 389, 769,
		   1543, 3079, 6151, 12289, 24593,
		   49157, 98317, 196613, 393241, 786433,
		   1572869, 3145739, 6291469, 12582917, 25165843,
		   50331653, 100663319, 201326611, 402653189, 805306457,
		   1610612741, 3221225473, 4294967291
		};
		const unsigned long* first = __stl_prime_list;//首尾指针
		const unsigned long* last = __stl_prime_list + __stl_num_primes;

		const unsigned long* pos = lower_bound(first, last, n);
		return pos == last ? *(last - 1) : *pos;// 传出最接近n的素数

	}
	// K 为 T 中key的类型
	// T 可能是键值对，也可能是K
	// KeyOfT: 从T中提取key
	// Hash将key转化为整形，因为hash函数使用除留余数法
	template<class K, class T, class KeyOfT, class Hash= HashFunc<K>>
	class HashTable
	{

		typedef HashNode<T> Node;
	public:
		HashTable()
		{
			_tables.resize(10, nullptr);
		}

		// 哈希桶的销毁
		~HashTable()
		{
			for (int a = 0; a < _tables.capacity(); a++)
			{
				Node* cur = _tables[a];
				Node* temp = nullptr;
				while (cur != nullptr)
				{
					temp = cur->_next;
					delete cur;
					cur = temp;
				}
				_tables[a] = nullptr;
			}
		}

		// 插入值为data的元素，如果data存在则不插入
		bool Insert(const T& data)
		{
			if ((float)_n / _tables.capacity() >= 1)
			{
				vector<Node*> newtable((unsigned long)__stl_next_prime(_tables.capacity() * 2));
				_tables.swap(newtable);
				for (int i = 0; i < newtable.capacity(); i++)
				{
					if (newtable[i] != nullptr)
					{
						Node* cur = newtable[i];
						Node* temp = nullptr;
						while (cur != nullptr)
						{
							temp = cur->_next;
							Insert(cur->_data);
							cur = temp;
						}
					}
				}
			}

			Hash hashf;
			KeyOfT kot;
			int setp = (int)(hashf(kot(data)) % _tables.capacity());//hash函数
			if (setp < 0)
				setp += (int)_tables.capacity();


			if (_tables[setp] == nullptr)
			{
				_tables[setp] = new Node(data);
				_n++;
				return true;
			}
			else
			{
				Node* cur = _tables[setp];
				Node* pre = cur;
				while (cur != nullptr)
				{
					if (kot(cur->_data) == kot(data))
						return false;
					pre = cur;
					cur = cur->_next;
				}
				pre->_next = new Node(data);
				_n++;
				return true;

			}
			return false;
		}

		// 在哈希桶中查找值为key的元素，存在返回true否则返回false﻿
		bool Find(const K& key)
		{
			Hash hashf;
			KeyOfT kot;
			int setp = hashf(key) % _tables.capacity();
			if (setp < 0)
				setp += (int)_tables.capacity();

			Node* temp = _tables[setp];
			while (temp != nullptr)
			{
				if (kot(temp->_data) == key)
					return true;
				else
					temp = temp->_next;
			}
			return false;
		}

		// 哈希桶中删除key的元素，删除成功返回true，否则返回false
		bool Erase(const K& key)
		{
			Hash hashf;
			KeyOfT kot;
			int setp = hashf(key) % _tables.capacity();
			if (setp < 0)
				setp += (int)_tables.capacity();

			Node* temp = _tables[setp];
			Node* pre = temp;

			while (temp != nullptr)
			{
				if (kot(temp->_data) == key)
				{
					if (pre == temp)
					{
						_tables[setp] = _tables[setp]->_next;
						delete temp;
					}
					else
					{
						pre->_next = temp->_next;
						delete temp;
					}
					--_n;
					return true;
				}
				else
				{
					pre = temp;
					temp = temp->_next;
				}
			}
			return false;
		}

		void Print()
		{
			KeyOfT kot;
			for (int a = 0; a < _tables.capacity(); a++)
			{
				Node* i = _tables[a];
				if (i == nullptr)
					cout << "nullptr";
				while(i!=nullptr)
				{
					cout << kot(i->_data) << " -> ";
					i = i->_next;
				}
				cout<<endl;
			}
		}

	private:
		vector<Node*> _tables;  // 指针数组
		size_t _n = 0;			// 表中存储数据个数
	};
}